package com.example.notification;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.ColorSpace;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import java.io.File;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.send_notice).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Build.VERSION.SDK_INT >= 26) {
                    // 如果sdk版本大于26
                    Intent intent = new Intent(MainActivity.this, NotificationActivity.class);
                    PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);

                    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                    String id = "channel_1";
                    int importance = NotificationManager.IMPORTANCE_DEFAULT;
                    String description = "这是一个通知.";
                    NotificationChannel channel = new NotificationChannel(id, description, importance);
                    // 为 NotificationChannel 添加属性
                    //channel.enableLights(true); // 震动
                    //channel.enableVibration(true); // 提示音

                    manager.createNotificationChannel(channel); // 添加 channel
                    Notification notification = new NotificationCompat.Builder(MainActivity.this, id)
                            .setContentTitle("This is content title")
                            .setContentText("This is content content")
                            .setWhen(System.currentTimeMillis())
                            .setSmallIcon(R.mipmap.ic_launcher) // 设置小图标，只能用alpha图层的图片进行设置
                            // 设置大图标, 下拉状态栏后大图标会展示出来
                            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background))
                            .setContentIntent(pendingIntent)
                            .setSound(Uri.fromFile(new File("/system/media/audio/ringtones/Luna/Luna.ogg")))
                            .setVibrate(new long[]{0,1000,1000,1000})
                            .setLights(Color.GREEN,1000,1000)
                            .setStyle(new NotificationCompat.BigTextStyle().bigText("...长文本字符串...长文本字符串...长文本字符串...长文本字符串...长文本字符串...长文本字符串...长文本字符串...长文本字符串...长文本字符串...长文本字符串...长文本字符串"))
                            //.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.drawable.big_image)))
                            .setPriority(NotificationCompat.PRIORITY_HIGH)
                            .build();
                    manager.notify(1, notification);

                } else {
                    // sdk 版本小于26
                    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                    Notification notification = new NotificationCompat.Builder(MainActivity.this)
                            .setContentTitle("This is content title")
                            .setContentText("This is content content")
                            .setWhen(System.currentTimeMillis())
                            .setSmallIcon(R.mipmap.ic_launcher) // 设置小图标，只能用alpha图层的图片进行设置
                            // 设置大图标, 下拉状态栏后大图标会展示出来
                            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background))
                            .build();
                    manager.notify(1, notification);
                }
            }
        });
    }
}